<template>
  <div class="result-page">
    <header class="header">
      <button class="back-btn" @click="handleBack">
        <span class="back-icon">←</span>
      </button>
      <h1 class="title">FAMWELL</h1>
      <button class="menu-btn">
        <span></span>
        <span></span>
        <span></span>
      </button>
    </header>

    <div class="result-container">
      <div v-if="Object.keys(drugs).length > 0">
        <div class="result-status negative">
          {{ isNegative ? 'RESULT' : 'RESULT' }}
        </div>   

        <div class="drug-list">
          <div class="indicator-labels">
          <span class="label-t">T</span>
          <span class="label-c">C</span>
        </div>
          <div v-for="(value, name) in drugs" :key="name" class="drug-item">
            <div class="drug-icon" :class="{ 
              'negative': value.c === 1 && value.t === 1, 
              'warning': value.c === 0, 
              'positive': value.c === 1 && value.t === 0 
            }">
              <span v-if="value.c === 1 && value.t === 1">-</span>
              <span v-if="value.c === 1 && value.t === 0">+</span>
              <span v-if="value.c === 0">!</span>
            </div>
            <div class="drug-name">{{ name }}</div>
            <div class="drug-indicator">
              <div class="indicator-bar">
                <div class="indicator-segment t-line" v-if="value.t === 1"></div>
                <div class="indicator-segment c-line" v-if="value.c === 1"></div>
              </div>
            </div>
          </div>
        </div>
      </div>
      <div v-else class="no-data">
        <p>No detection results</p>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, computed, onMounted } from 'vue';
import { useRoute, useRouter } from 'vue-router';

const route = useRoute();
const router = useRouter();

// This would normally come from API or props
// From route params get drugs data
const drugs = ref(history.state?.drugs ? JSON.parse(history.state.drugs) : {});

// According to drugs data calculate if it's negative
const isNegative = computed(() => Object.values(drugs.value).every(value => value === 1));

const handleBack = () => {
  history.state.drugs = null;
  router.back();
};

onMounted(() => {
  
});
</script>

<style scoped>
.back-btn {
  background: none;
  border: none;
  padding: 5px;
  cursor: pointer;
  color: #000080;
  font-size: 24px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.back-icon {
  display: block;
  line-height: 1;
}

.result-page {
  min-height: 100vh;
  background-color: #FFF5F3;
  display: flex;
  flex-direction: column;
  padding: 10px 15px;
  position: relative;
  width: 100%;
  box-sizing: border-box;
}

.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 30px;
}

.title {
  font-size: 24px;
  font-weight: bold;
  color: #000080;
  margin: 0;
}

.menu-btn {
  background: none;
  border: none;
  padding: 5px;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.menu-btn span {
  display: block;
  width: 20px;
  height: 2px;
  background-color: #000;
}

.result-container {
  display: flex;
  flex-direction: column;
  gap: 20px;
  width: 100%; /* Maintain 100% of the parent which is now 3x wider */
}

.result-status {
  padding: 15px;
  font-weight: bold;
  color: white;
  border-radius: 8px;
  font-size: 18px;
  align-self: flex-start;
  width: 100%;
  box-sizing: border-box;
  font-size: 24px;
  display: flex;
  align-items: center;
  justify-content: center;
  text-align: center;
}

.negative {
  background-color: #4CAF50;
}

.inconclusive {
  background-color: #D32F2F;
}

.drug-list {
  background-color: white;
  border-radius: 8px;
  padding: 20px 30px; /* Increased padding for better spacing */
  display: flex;
  flex-direction: column;
  gap: 20px; /* Increased gap for better spacing */
  width: 100%;
  box-sizing: border-box;
  max-width: 100%;
}

.drug-item {
  display: flex;
  align-items: center;
  gap: 30px; /* Increased gap between drug item elements */
}

.drug-icon {
  width: 30px;
  height: 30px;
  border-radius: 50%;
  background-color: #4CAF50;
  color: white;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: bold;
  min-width: 30px;
  min-height: 30px;
  font-size: 28px;
  position: relative;
}

.drug-icon span {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  height: 100%;
  position: absolute;
  top: 0;
  left: 0;
}

.drug-icon.positive {
  background-color: #4CAF50;
}

.drug-icon.negative {
  background-color: #D32F2F;
}

.drug-icon.warning {
  background-color: #FFA000;
}

.drug-name {
  min-width: 50px;
  width: 50px;
  font-weight: bold;
  font-size: 18px;
  margin-left: -10px;
}

.drug-indicator {
  flex: 1;
  min-width: 0;
}

.indicator-bar {
  height: 30px; /* Increased height */
  border: 2px solid #ccc; /* Thicker border */
  width: 100%; /* Ensure full width */
  position: relative;
}

.indicator-segment {
  position: absolute;
  top: 0;
  bottom: 0;
  width: 100%;
}

/* C line (always at the same position) */
.indicator-segment.c-line:after {
  content: '';
  position: absolute;
  top: 0;
  bottom: 0;
  width: 5px; /* Increased width of line */
  background-color: #8B0000; /* Deep red color for C line */
  right: 30%; /* Position at 40% from left - fixed position */
}

/* T line (on the right side) */
.indicator-segment.t-line:after {
  content: '';
  position: absolute;
  top: 0;
  bottom: 0;
  width: 5px; /* Increased width of line */
  background-color: #8B0000; /* Bright red color for T line */
  left: 30%; /* Position at 40% from right */
}

.no-data {
  background-color: white;
  border-radius: 8px;
  padding: 20px;
  text-align: center;
  font-size: 18px;
  color: #666;
  width: 100%;
  box-sizing: border-box;
}

.indicator-labels {
  display: flex;
  justify-content: space-between;
  padding: 0px 40px 0px 180px;
  font-weight: bold;
  font-size: 20px;
  color: #8B0000;
}

.label-c {
  width: 30px;
  text-align: center;
  margin-left: -10px;
}
.label-t {
  width: 30px;
  text-align: center;
  margin-left: -10px;
}
</style>